package com.example.demo.command;

import com.example.demo.controller.TestController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.Availability;
import org.springframework.shell.standard.*;

@Slf4j
@ShellComponent
@ShellCommandGroup("operate ctl")
public class TestCommand {

    private boolean connected;

    @Autowired
    private TestController testController;

    @ShellMethod("test api .")
    public String test() {
        return testController.test();
    }

    @ShellMethod("连接服务器，格式：connect ip port")
    public String conn(@ShellOption(defaultValue = "localhost") String ip,
                          @ShellOption(defaultValue = "8080") int port) {
        return String.format("连接服务成功：%s:%s", ip, port);
    }


    @ShellMethod("Connect to the server.")
    public String login(String user, String password) {
        if (StringUtils.equals(user,"admin") && StringUtils.equals(password,"123456")){
            connected = true;
            return "login success";
        }else {
            connected = false;
            return "user or password is error";
        }
    }

    @ShellMethod("Download the nuclear codes.")
    @ShellMethodAvailability("downloadAvailability")
    public void download() {

    }

    public Availability downloadAvailability() {
        return connected
                ? Availability.available()
                : Availability.unavailable("you are not connected");
    }
}